<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\ArticleComments;
use App\Models\Category;
use App\Models\Link;
use App\Models\Quotation;
use Illuminate\Http\Request;


class IndexController extends Controller
{
    public $category;//类别
    public $links;//友情链接
    public $news;//最新
    public $hots;//最热
    public $randoms;//随机1
    public $randoms2;//随机2
    public $eights;//
    public $recommend;//推荐
    public $cates;//类别
    public $quotation;//语录
    public $user;//登录信息

    public function __construct()
    {
        //类别列表
        $category = Category::all('id','name')->toArray();
        //友情链接
        $links = Link::all('id','name','url')->toArray();
        //最新
        $news = Article::orderBy('id','desc')->take(5)->get();
        //最热
        $hots = Article::orderBy('pv','desc')->take(1)->get();
        //随机
        $randoms = Article::inRandomOrder()->take(3)->get();//相关推荐
        //随机2
        $randoms2 = Article::inRandomOrder()->take(1)->get();//相关推荐
        $eights =  Article::inRandomOrder()->take(8)->get();//相关推荐
        foreach ($news as $new){
            if($new->images){
                $images = explode(',',$new->images);
                if($images){
                    $new->images = imageChange($images);
                }else{
                    $new->images = [];
                }
                $new->image_count = count($images);
            }else{
                $images = imageChange($new->main_image);
                if($images){
                    $images = env('IMAGE_PATH').$images;
                    $new->image_count = 1;
                    $new->images = [$images];
                }else{
                    $new->image_count = 0;
                    $new->images = [];
                }
            }
            $new->main_image = env('QINIU_IMAGE').$new->main_image;
            $new->describe = getShowText($new->content);

        }
        foreach ($hots as $hot){
            if($hot->images){
                $images = explode(',',$hot->images);
                if($images){
                    $hot->images = imageChange($images);
                }else{
                    $hot->images = [];
                }
                $hot->image_count = count($images);
            }else{
                $images = imageChange($hot->main_image);
                if($images){
                    $images = env('IMAGE_PATH').$images;
                    $hot->image_count = 1;
                    $hot->images = [$images];
                }else{
                    $hot->image_count = 0;
                    $hot->images = [];
                }
            }
            $hot->main_image = env('QINIU_IMAGE').$hot->main_image;
            $hot->describe = getShowText($hot->content);
        }
        foreach ($randoms as $random){
            if($random->images){
                $images = explode(',',$random->images);
                if($images){
                    $random->images = imageChange($images);
                }else{
                    $random->images = [];
                }
                $random->image_count = count($images);
            }else{
                $images = imageChange($random->main_image);
                if($images){
                    $images = env('IMAGE_PATH').$images;
                    $random->image_count = 1;
                    $random->images = [$images];
                }else{
                    $random->image_count = 0;
                    $random->images = [];
                }
            }
            $random->main_image = env('QINIU_IMAGE').$random->main_image;
            $random->describe = getShowText($random->content);
        }
        foreach ($randoms2 as $random2){
            if($random2->images){
                $images = explode(',',$random2->images);
                if($images){
                    $random2->images = imageChange($images);
                }else{
                    $random2->images = [];
                }
                $random2->image_count = count($images);
            }else{
                $images = imageChange($random2->main_image);
                if($images){
                    $images = env('IMAGE_PATH').$images;
                    $random2->image_count = 1;
                    $random2->images = [$images];
                }else{
                    $random2->image_count = 0;
                    $random2->images = [];
                }
            }
            $random2->main_image = env('QINIU_IMAGE').$random2->main_image;
            $random2->describe = getShowText($random2->content);
        }
        foreach ($eights as $eight){
            if($eight->images){
                $images = explode(',',$eight->images);
                if($images){
                    $eight->images = imageChange($images);
                }else{
                    $eight->images = [];
                }
                $eight->image_count = count($images);
            }else{
                $images = imageChange($eight->main_image);
                if($images){
                    $images = env('IMAGE_PATH').$images;
                    $eight->image_count = 1;
                    $eight->images = [$images];
                }else{
                    $eight->image_count = 0;
                    $eight->images = [];
                }
            }
            $eight->main_image = env('QINIU_IMAGE').$eight->main_image;
            $eight->describe = getShowText($eight->content);
        }
        $cates = Category::orderBy('id','desc')->take(4)->get();
        //查询语录信息
        $quotation = Quotation::inRandomOrder()->first();
        $recommend = Article::orderBy('pv','desc')->take(10)->get();//推荐
        $this->category = $category;
        $this->links = $links;
        $this->news = $news;
        $this->hots = $hots;
        $this->randoms = $randoms;
        $this->randoms2 = $randoms2;
        $this->eights = $eights;
        $this->cates = $cates;
        $this->quotation = $quotation;
        $this->recommend = $recommend;
    }


    //首页
    public function home()
    {
        $news = Article::orderBy('id','desc')->paginate(20)->toJson();
        $hots = Article::orderBy('pv','desc')->paginate(20)->toJson();

        $news = json_decode($news,true);
        $hots = json_decode($hots,true);

        return view('home',[
            'category'=>$this->category,
            'category_id'=>0,
            'links'=>$this->links,
            'news'=>$news,
            'hots'=>$hots,
        ]);
    }

    //首页2
    public function home2()
    {
        $news = Article::orderBy('id','desc')->paginate(20)->toJson();
        $hots = Article::orderBy('pv','desc')->paginate(20)->toJson();

        $news = json_decode($news,true);
        $hots = json_decode($hots,true);
        return view('home2',[
            'category'=>$this->category,
            'category_id'=>0,
            'links'=>$this->links,
            'news'=>$news,
            'hots'=>$hots,
        ]);
    }

    //首页3
    public function home3(Request $request)
    {
        $user = session('user');
        //最新
        $news = $this->news;
        //最热
        $hots = $this->hots;
        //随机
        $randoms = $this->randoms;//相关推荐
        //随机2
        $randoms2 = $this->randoms2;//相关推荐

        $eights =  $this->eights;//相关推荐

        $cates = $this->cates;
        //查询语录信息
        $quotation = $this->quotation;

        return view('home3',[
            'category'=>$this->category,
            'category_id'=>0,
            'links'=>$this->links,
            'news'=>$news,
            'hots'=>$hots,
            'randoms'=>$randoms,
            'randoms2'=>$randoms2,
            'eights'=>$eights,
            'cates'=>$cates,
            'quotation'=>$quotation,
            'user'=>$user,
        ]);
    }

    //双栏板式
    public function page2(Request  $request)
    {
        $param = $request->all();
        $s = isset($param['s'])?$param['s']:"";
        $articles = Article::where('title','like','%'.$s.'%')->orderBy('id','desc')->get();
        foreach ($articles as $article){
            if($article->images){
                $images = explode(',',$article->images);
                if($images){
                    $article->images = imageChange($images);
                }else{
                    $article->images = [];
                }
                $article->image_count = count($images);
            }else{
                $images = imageChange($article->main_image);
                if($images){
                    $images = env('IMAGE_PATH').$images;
                    $article->image_count = 1;
                    $article->images = [$images];
                }else{
                    $article->image_count = 0;
                    $article->images = [];
                }
            }
            $article->main_image = env('QINIU_IMAGE').$article->main_image;
            $article->describe = getShowText($article->content);
        }
        //最新
        $news = $this->news;
        //最热
        $hots = $this->hots;
        //随机
        $randoms = $this->randoms;//相关推荐
        //查询语录信息
        $quotation = $this->quotation;

        return view('page2',[
            'category'=>$this->category,
            'category_id'=>0,
            'links'=>$this->links,
            'news'=>$news,
            'hots'=>$hots,
            'randoms'=>$randoms,
            'quotation'=>$quotation,
            'articles'=>$articles,
        ]);
    }

    //搜索
    public function search(Request  $request)
    {
        $param = $request->all();
        //最新
        $news = $this->news;
        //最热
        $hots = $this->hots;
        //随机
        $randoms = $this->randoms;//相关推荐

        $s = isset($param['s'])?$param['s']:"";
        $articles = Article::where('title','like','%'.$s.'%')->orderBy('id','desc')->get();
        foreach ($articles as $article){
            if($article->images){
                $images = explode(',',$article->images);
                if($images){
                    $article->images = imageChange($images);
                }else{
                    $article->images = [];
                }
                $article->image_count = count($images);
            }else{
                $images = imageChange($article->main_image);
                if($images){
                    $images = env('IMAGE_PATH').$images;
                    $article->image_count = 1;
                    $article->images = [$images];
                }else{
                    $article->image_count = 0;
                    $article->images = [];
                }
            }
            $article->main_image = env('QINIU_IMAGE').$article->main_image;
            $article->describe = getShowText($article->content);
        }

        //查询语录信息
        $quotation = $this->quotation;

        return view('page2',[
            'category'=>$this->category,
            'category_id'=>0,
            'links'=>$this->links,
            'articles'=>$articles,
            'news'=>$news,
            'hots'=>$hots,
            'randoms'=>$randoms,
            'quotation'=>$quotation,
        ]);
    }


    //对应类别下的文章
    public function category(Request $request)
    {
        $param = $request->all();

        $id = isset($param['id'])?$param['id']:"";
        $category = Category::where('id',$id)->first();
        if(!$category){
            $localhost = env("APP_URL");
            $key = rand(1,2);
            $img = $localhost."/404/404_".$key.".webp";
            return view("errors.404", [
                'img' => $img,
            ]);
        }
        $article = Article::where('category_id',$id)->orderBy('id','desc')->paginate(15);

        return view('category',[
            'category'=>$this->category,
            'categoryInfo'=>$category,
            'category_id'=>$id,
            'article'=>$article,
            'links'=>$this->links,
        ]);
    }

    //对应类别下的文章
    public function category2(Request  $request)
    {
        $param = $request->all();
        $category_id = isset($param['category_id'])?$param['category_id']:0;
        $cateInfo = Category::where('id',$category_id)->first();
        //最新
        $news = $this->news;
        //最热
        $hots = $this->hots;
        $articles = Article::where('category_id',$category_id)->orderBy('id','desc')->get();//对应类别下的文章

        foreach ($articles as $article){

            if($article->images){
                $images = explode(',',$article->images);
                if($images){
                    $article->images = imageChange($images);
                }else{
                    $article->images = [];
                }
                $article->image_count = count($images);
            }else{
                $images = imageChange($article->main_image);
                if($images){
                    $images = env('IMAGE_PATH').$images;
                    $article->image_count = 1;
                    $article->images = [$images];
                }else{
                    $article->image_count = 0;
                    $article->images = [];
                }
            }
            $article->main_image = env('QINIU_IMAGE').$article->main_image;
            $article->describe = getShowText($article->content);
        }

        $cates = $this->cates;

        return view('category2',[
            'category'=>$this->category,
            'category_id'=>$category_id,
            'news'=>$news,
            'hots'=>$hots,
            'cates'=>$cates,
            'cateInfo'=>$cateInfo,
            'articles'=>$articles,
        ]);
    }


    //详情页
    public function details(Request $request)
    {
        $param = $request->all();
        $id = isset($param['id'])?$param['id']:"";
        $article = Article::where('id',$id)->first();
        if(!$article){
            $localhost = env("APP_URL");
            $key = rand(1,2);
            $img = $localhost."/404/404_".$key.".webp";
            return view("errors.404", [
                'img' => $img,
            ]);
        }
        $article->content = imageChange($article->content);
        $local = env('APP_URL');
        $article->content = str_replace("http://43.139.118.68",$local,$article->content);
        //给文章pv+1
        $article->pv += 1;
        $article->save();



        //查询语录信息
        $quotation = $this->quotation;

        $hots = $this->hots;//相关推荐

        $recommend = $this->recommend;


        $category = Category::where('id',$article->category_id)->first();


        return view('article',[
            'category'=>$this->category,
            'categoryInfo'=>$category,
            'category_id'=>$article->category_id,
            'article'=>$article,
            'quotation'=>$quotation,
            'links'=>$this->links,
            'hots'=>$hots,
            'recommend'=>$recommend,
        ]);
    }

    //文章详情2
    public function details2(Request  $request)
    {
        $param = $request->all();
        $id = isset($param['id'])?$param['id']:"";
        $article = Article::where('id',$id)->first();
        if(!$article){
            $localhost = env("APP_URL");
            $key = rand(1,2);
            $img = $localhost."/404/404_".$key.".webp";
            return view("errors.404", [
                'img' => $img,
            ]);
        }

        $article->describe =  getStringText($article->content);
        $article->content = imageChange($article->content);
        $local = env('APP_URL');
        $article->content = str_replace("http://43.139.118.68",$local,$article->content);
        //给文章pv+1
        $article->pv += 1;
        $article->save();

        //查询语录信息
        $quotation = $this->quotation;

        $hots = $this->hots;//相关推荐,klo

        $recommend = $this->recommend;

        //获取文章评论
        $comments = ArticleComments::where('article_id',$id)->get();

        $category = Category::where('id',$article->category_id)->first();


        return view('article2',[
            'category'=>$this->category,
            'categoryInfo'=>$category,
            'category_id'=>$article->category_id,
            'article'=>$article,
            'quotation'=>$quotation,
            'links'=>$this->links,
            'hots'=>$hots,
            'recommend'=>$recommend,
            'comments'=>$comments,
        ]);
    }


}
